package com.yunkont.pigcontrol.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.yunkont.pigcontrol.R;
import com.yunkont.pigcontrol.app.BaseApplicaiton;
import com.yunkont.pigcontrol.bean.AllPigBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by bomei on 2017/9/23.
 */

public class AllPigActivity extends AppCompatActivity {

    @Bind(R.id.ryclerview)
    RecyclerView ryclerview;
    private  List<AllPigBean.EmbeddedBean.PigConfigsBean> pigConfigs = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allpig);
        ButterKnife.bind(this);
        initView();
    }


    private void initView() {
        ryclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        AllPigBean allPigBean = BaseApplicaiton.getInstance().getAllPigBean();
        if(allPigBean == null || allPigBean.get_embedded() == null){
            Toast.makeText(this,"未获取到猪信息",Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        pigConfigs = allPigBean.get_embedded().getPigConfigs();
        ryclerview.setAdapter(new RecyclerView.Adapter() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(AllPigActivity.this).inflate(R.layout.item_ry_shoosepig, parent, false);
                return new MyViewHolder(view);
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                MyViewHolder holder1 = (MyViewHolder) holder;
                holder1.tvPid.setText(pigConfigs.get(position).getPid());
            }

            @Override
            public int getItemCount() {
                return pigConfigs == null ? 0 : pigConfigs.size();
            }
        });
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.tvPid)
        TextView tvPid;

        MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent();
                    intent.putExtra("pigId",pigConfigs.get(getAdapterPosition()).getPid());
                    intent.putExtra("linkUrl",pigConfigs.get(getAdapterPosition()).get_links().getSelf().getHref());
                    setResult(RESULT_OK,intent);
                    finish();
                }
            });
        }
    }
}
